<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "organization".
 *
 * @property int $id 机构
 * @property int $user_id
 * @property string $name 机构名称
 * @property string $avatar_path 机构头像
 * @property string $desc 机构简介
 * @property int $is_delete 是否删除
 * @property int $created_at 创建时间戳
 * @property int $updated_at 更新时间戳
 */
class OrganizationModel extends \backend\base\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'organization';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id', 'is_delete', 'created_at', 'updated_at'], 'integer'],
            [['desc'], 'string'],
            [['name'], 'string', 'max' => 45],
            [['avatar_path','business_license_image','permission_image'], 'string', 'max' => 255],
            [['business_license_code','contact_person','job','telephone','tel','email','alipay_account','public_account'], 'string', 'max' => 32],
        ];
    }

    public function scenarios()
    {
        return array_merge(parent::scenarios(),[
            'add' => ['desc','name','avatar_path','business_license_image','business_license_code','contact_person','job','telephone','tel','email','permission_image'],
            'edit' => ['desc','name','avatar_path','business_license_image','business_license_code','contact_person','job','telephone','tel','email','permission_image'],
        ]);
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id' => '用户ID',
            'name' => '机构名称',
            'avatar_path' => '机构LOGO',
            'desc' => '机构介绍',
            'business_license_image' => '营业执照图片',
            'business_license_code' => '营业执照注册代码',
            'contact_person' => '负责人姓名',
            'job' => '职务',
            'telephone' => '手机号',
            'tel' => '座机',
            'email' => '邮箱',
            'permission_image' => '企业开户许可照片',
            'is_delete' => '删除',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }
    /**
     * 保存前
     * @param bool $insert
     * @return bool
     */
    public function beforeSave($insert)
    {
        if(parent::beforeSave($insert)){
            $time = time();
            if($this->isNewRecord){
                $this->created_at = $time;
                $this->is_delete = 0;
                $this->is_checked = 1;
            }else{
                //修改图片,删除旧图片
                if($this->avatar_path != $this->getOldAttribute('avatar_path') && $this->getOldAttribute('avatar_path')){
                    parent::$del_img_list[] = $this->getOldAttribute('avatar_path');
                }
            }
            $this->updated_at = $time;
            return true;
        }
        return false;
    }

    public function getUser(){
        return $this->hasOne(UserModel::className(),['id'=>'user_id']);
    }
    public function getImageThumb(){
        return !empty($this->avatar_path) ? ImageSize::resize($this->avatar_path,200,200) : '';
    }

    //机构列表
    public static function orgAll()
    {
        $org = self::find()->select(['id','name']);
        if(Yii::$app->user->id != 1) {
            $org->where(['user_id' => Yii::$app->user->id]);
        }
        $org = $org->asArray()->all();
        return $org;
    }
}
